﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Media
Namespace SimpleChart.Charts

    Public Module Helper
        'Sub New()
        'End Sub
        Private colorList As New List(Of Color)()
        Private darkColorList As New List(Of Color)()
        Private addedList As New Dictionary(Of String, Color)()

        Private lockObject As New Object()


        Const totalColorIndex As Integer = 139


        Sub New()
            colorList.Add(Colors.AliceBlue)
            colorList.Add(Colors.AntiqueWhite)
            colorList.Add(Colors.Aqua)
            colorList.Add(Colors.Aquamarine)
            colorList.Add(Colors.Azure)
            colorList.Add(Colors.Beige)
            colorList.Add(Colors.Bisque)
            colorList.Add(Colors.Black)
            colorList.Add(Colors.BlueViolet)
            colorList.Add(Colors.Brown)
            colorList.Add(Colors.BurlyWood)
            colorList.Add(Colors.CadetBlue)
            colorList.Add(Colors.Chartreuse)
            colorList.Add(Colors.Chocolate)
            colorList.Add(Colors.Coral)
            colorList.Add(Colors.CornflowerBlue)
            colorList.Add(Colors.Cornsilk)
            colorList.Add(Colors.Crimson)
            colorList.Add(Colors.Cyan)
            colorList.Add(Colors.DarkBlue)
            colorList.Add(Colors.DarkCyan)
            colorList.Add(Colors.DarkGoldenrod)
            colorList.Add(Colors.DarkGray)
            colorList.Add(Colors.DarkGreen)
            colorList.Add(Colors.DarkKhaki)
            colorList.Add(Colors.DarkMagenta)
            colorList.Add(Colors.DarkOliveGreen)
            colorList.Add(Colors.DarkOrange)
            colorList.Add(Colors.DarkOrchid)
            colorList.Add(Colors.DarkRed)
            colorList.Add(Colors.DarkSalmon)
            colorList.Add(Colors.DarkSeaGreen)
            colorList.Add(Colors.DarkSlateBlue)
            colorList.Add(Colors.DarkSlateGray)
            colorList.Add(Colors.DarkTurquoise)
            colorList.Add(Colors.DarkViolet)
            colorList.Add(Colors.DeepPink)
            colorList.Add(Colors.DeepSkyBlue)
            colorList.Add(Colors.DimGray)
            colorList.Add(Colors.DodgerBlue)
            colorList.Add(Colors.Firebrick)
            colorList.Add(Colors.FloralWhite)
            colorList.Add(Colors.ForestGreen)
            colorList.Add(Colors.Fuchsia)
            colorList.Add(Colors.Gainsboro)
            colorList.Add(Colors.GhostWhite)
            colorList.Add(Colors.Gold)
            colorList.Add(Colors.Goldenrod)
            colorList.Add(Colors.Gray)
            colorList.Add(Colors.Green)
            colorList.Add(Colors.GreenYellow)
            colorList.Add(Colors.Honeydew)
            colorList.Add(Colors.HotPink)
            colorList.Add(Colors.IndianRed)
            colorList.Add(Colors.Indigo)
            colorList.Add(Colors.Ivory)
            colorList.Add(Colors.Khaki)
            colorList.Add(Colors.Lavender)
            colorList.Add(Colors.LavenderBlush)
            colorList.Add(Colors.LawnGreen)
            colorList.Add(Colors.LemonChiffon)
            colorList.Add(Colors.LightBlue)
            colorList.Add(Colors.LightCoral)
            colorList.Add(Colors.LightCyan)
            colorList.Add(Colors.LightGoldenrodYellow)
            colorList.Add(Colors.LightGray)
            colorList.Add(Colors.LightGreen)
            colorList.Add(Colors.LightPink)
            colorList.Add(Colors.LightSalmon)
            colorList.Add(Colors.LightSeaGreen)
            colorList.Add(Colors.LightSkyBlue)
            colorList.Add(Colors.LightSlateGray)
            colorList.Add(Colors.LightSteelBlue)
            colorList.Add(Colors.LightYellow)
            colorList.Add(Colors.Lime)
            colorList.Add(Colors.LimeGreen)
            colorList.Add(Colors.Linen)
            colorList.Add(Colors.Magenta)
            colorList.Add(Colors.Maroon)
            colorList.Add(Colors.MediumAquamarine)
            colorList.Add(Colors.MediumBlue)
            colorList.Add(Colors.MediumOrchid)
            colorList.Add(Colors.MediumPurple)
            colorList.Add(Colors.MediumSeaGreen)
            colorList.Add(Colors.MediumSlateBlue)
            colorList.Add(Colors.MediumSpringGreen)
            colorList.Add(Colors.MediumTurquoise)
            colorList.Add(Colors.MediumVioletRed)
            colorList.Add(Colors.MidnightBlue)
            colorList.Add(Colors.MintCream)
            colorList.Add(Colors.MistyRose)
            colorList.Add(Colors.Moccasin)
            colorList.Add(Colors.NavajoWhite)
            colorList.Add(Colors.Navy)
            colorList.Add(Colors.OldLace)
            colorList.Add(Colors.Olive)
            colorList.Add(Colors.OliveDrab)
            colorList.Add(Colors.Orange)
            colorList.Add(Colors.OrangeRed)
            colorList.Add(Colors.Orchid)
            colorList.Add(Colors.PaleGoldenrod)
            colorList.Add(Colors.PaleGreen)
            colorList.Add(Colors.PaleTurquoise)
            colorList.Add(Colors.PaleVioletRed)
            colorList.Add(Colors.PapayaWhip)
            colorList.Add(Colors.PeachPuff)
            colorList.Add(Colors.Peru)
            colorList.Add(Colors.Pink)
            colorList.Add(Colors.Plum)
            colorList.Add(Colors.PowderBlue)
            colorList.Add(Colors.Purple)
            colorList.Add(Colors.Red)
            colorList.Add(Colors.RosyBrown)
            colorList.Add(Colors.RoyalBlue)
            colorList.Add(Colors.SaddleBrown)
            colorList.Add(Colors.Salmon)
            colorList.Add(Colors.SandyBrown)
            colorList.Add(Colors.SeaGreen)
            colorList.Add(Colors.SeaShell)
            colorList.Add(Colors.Sienna)
            colorList.Add(Colors.Silver)
            colorList.Add(Colors.SkyBlue)
            colorList.Add(Colors.SlateBlue)
            colorList.Add(Colors.SlateGray)
            colorList.Add(Colors.Snow)
            colorList.Add(Colors.SpringGreen)
            colorList.Add(Colors.SteelBlue)
            colorList.Add(Colors.Tan)
            colorList.Add(Colors.Teal)
            colorList.Add(Colors.Thistle)
            colorList.Add(Colors.Tomato)
            colorList.Add(Colors.Transparent)
            colorList.Add(Colors.Turquoise)
            colorList.Add(Colors.Violet)
            colorList.Add(Colors.Wheat)
            colorList.Add(Colors.White)
            colorList.Add(Colors.WhiteSmoke)
            colorList.Add(Colors.Yellow)
            colorList.Add(Colors.YellowGreen)

            'maxColorIndex = colorList.Count; 


            darkColorList.Add(Colors.MediumBlue)
            darkColorList.Add(Colors.Tomato)
            darkColorList.Add(Colors.Green)
            darkColorList.Add(Colors.Yellow)

            darkColorList.Add(Colors.SteelBlue)
            darkColorList.Add(Colors.SlateBlue)
            darkColorList.Add(Colors.SeaGreen)
            darkColorList.Add(Colors.PeachPuff)

            darkColorList.Add(Colors.MediumAquamarine)
            darkColorList.Add(Colors.MediumOrchid)
            darkColorList.Add(Colors.MediumPurple)
            darkColorList.Add(Colors.MediumSeaGreen)
            darkColorList.Add(Colors.MediumSlateBlue)
            darkColorList.Add(Colors.MediumSpringGreen)
            darkColorList.Add(Colors.MediumTurquoise)
            darkColorList.Add(Colors.MediumVioletRed)

            darkColorList.Add(Colors.DarkBlue)
            darkColorList.Add(Colors.DarkCyan)
            darkColorList.Add(Colors.DarkGoldenrod)
            darkColorList.Add(Colors.DarkGray)
            darkColorList.Add(Colors.DarkGreen)
            darkColorList.Add(Colors.DarkKhaki)
            darkColorList.Add(Colors.DarkMagenta)
            darkColorList.Add(Colors.DarkOliveGreen)
            darkColorList.Add(Colors.DarkOrange)
            darkColorList.Add(Colors.DarkOrchid)
            darkColorList.Add(Colors.DarkRed)
            darkColorList.Add(Colors.DarkSalmon)
            darkColorList.Add(Colors.DarkSeaGreen)
            darkColorList.Add(Colors.DarkSlateBlue)
            darkColorList.Add(Colors.DarkSlateGray)
            darkColorList.Add(Colors.DarkTurquoise)

            darkColorList.Add(Colors.DarkViolet)
        End Sub

        Public Function GetColorArrayLength() As Integer
            Return colorList.Count
        End Function

        Public Function GetColorByIndex(ByVal index As Integer) As Color
            Return colorList(index)
        End Function


        Public Function GetDarkColorByIndex(ByVal index As Integer) As Color
            If index >= darkColorList.Count Then
                index = New Random(Environment.TickCount).[Next](0, darkColorList.Count)
            End If
            Return darkColorList(index)
        End Function



        Public Function GetDarkColorByAI(ByVal index As Integer, ByVal label As String) As Color
            If index >= darkColorList.Count Then
                index = New Random(Environment.TickCount).[Next](0, darkColorList.Count - 1)
            End If

            If addedList.ContainsKey(label) Then
                Return addedList(label)
            End If

            addedList(label) = darkColorList(index)

            Return darkColorList(index)
        End Function


        Public Function GetColorBySeed(ByVal seed As Integer) As Color
            Dim rnd As New Random(seed)
            rnd.[Next](Environment.TickCount)

            Return colorList(rnd.[Next](0, 139))
        End Function


        Public Function GetRandomColorByte(ByVal min As Byte, ByVal max As Byte) As Byte
            Dim rnd As New Random()
            Dim b As Byte = Convert.ToByte(rnd.[Next](min, max))
            Return b
        End Function


        Public Function GetRandomColor() As Color
            Dim index As Integer
            SyncLock lockObject
                Dim rnd As New Random(Environment.TickCount)

                index = rnd.[Next](0, 139)
            End SyncLock

            Return colorList(index)
        End Function

    End Module

    Public Class Parser
#Region "Static Methods"


        ''' <summary> 
        ''' Returns a darker shade of the color by decreasing the brightness by the given intensity value 
        ''' </summary> 
        ''' <param name="color"></param> 
        ''' <param name="intensity"></param> 
        ''' <returns></returns> 
        Public Shared Function GetDarkerColor(ByVal color As System.Windows.Media.Color, ByVal intensity As [Double]) As System.Windows.Media.Color
            Dim darkerShade As New Color()
            intensity = If((intensity < 0 OrElse intensity > 1), 1, intensity)
            darkerShade.R = CType((color.R * intensity), Byte)
            darkerShade.G = CType((color.G * intensity), Byte)
            darkerShade.B = CType((color.B * intensity), Byte)
            darkerShade.A = 255
            Return darkerShade
        End Function
        Public Shared Function GetDarkerColor(ByVal color As System.Windows.Media.Color, ByVal intensityR As [Double], ByVal intensityG As [Double], ByVal intensityB As [Double]) As System.Windows.Media.Color
            Dim darkerShade As New Color()
            intensityR = If((intensityR < 0 OrElse intensityR > 1), 1, intensityR)
            intensityG = If((intensityG < 0 OrElse intensityG > 1), 1, intensityG)
            intensityB = If((intensityB < 0 OrElse intensityB > 1), 1, intensityB)
       
            darkerShade.R = CType((color.R * intensityR), Byte)
            darkerShade.G = CType((color.G * intensityG), Byte)
            darkerShade.B = CType((color.B * intensityB), Byte)
            darkerShade.A = 255
            Return darkerShade
        End Function
        ''' <summary> 
        ''' Returns a lighter shade of the color by increasing the brightness by the given intensity value 
        ''' </summary> 
        ''' <param name="color"></param> 
        ''' <param name="intensity"></param> 
        ''' <returns></returns> 
        Public Shared Function GetLighterColor(ByVal color As System.Windows.Media.Color, ByVal intensity As [Double]) As System.Windows.Media.Color
            Dim lighterShade As New Color()
            intensity = If((intensity < 0 OrElse intensity > 1), 1, intensity)
            lighterShade.R = CType((256 - ((256 - color.R) * intensity)), Byte)

            lighterShade.G = CType((256 - ((256 - color.G) * intensity)), Byte)
            lighterShade.B = CType((256 - ((256 - color.B) * intensity)), Byte)

            lighterShade.A = 255
            Return lighterShade
        End Function
        Public Shared Function GetLighterColor(ByVal color As System.Windows.Media.Color, ByVal intensityR As [Double], ByVal intensityG As [Double], ByVal intensityB As [Double]) As System.Windows.Media.Color
            Dim lighterShade As New Color()
            intensityR = If((intensityR < 0 OrElse intensityR > 1), 1, intensityR)
            intensityG = If((intensityG < 0 OrElse intensityG > 1), 1, intensityG)
            intensityB = If((intensityB < 0 OrElse intensityB > 1), 1, intensityB)
            lighterShade.R = CType((256 - ((256 - color.R) * intensityR)), Byte)
            lighterShade.G = CType((256 - ((256 - color.G) * intensityG)), Byte)
            lighterShade.B = CType((256 - ((256 - color.B) * intensityB)), Byte)

            lighterShade.A = 255
            Return lighterShade
        End Function

        Public Shared Sub GenerateDarkerGradientBrush(ByVal source As GradientBrush, ByVal result As GradientBrush, ByVal intensity As [Double])
            For Each grad As GradientStop In source.GradientStops
                Dim gs As New GradientStop()
                gs.Color = Parser.GetDarkerColor(grad.Color, intensity)
                gs.Offset = grad.Offset
                result.GradientStops.Add(gs)
            Next
        End Sub

        Public Shared Sub GenerateLighterGradientBrush(ByVal source As GradientBrush, ByVal result As GradientBrush, ByVal intensity As [Double])
            For Each grad As GradientStop In source.GradientStops
                Dim gs As New GradientStop()
                gs.Color = Parser.GetLighterColor(grad.Color, intensity)
                gs.Offset = grad.Offset
                result.GradientStops.Add(gs)
            Next
        End Sub

        Public Shared Function InvertColor(ByVal color As System.Windows.Media.Color) As System.Windows.Media.Color
            Dim newColor As New Color()
            newColor.A = 255
            
            newColor.R = CType((255 - color.R), Byte)
            newColor.G = CType((255 - color.G), Byte)
            newColor.B = CType((255 - color.B), Byte)
            Return newColor
        End Function

        Public Shared Function GetBrushIntensity(ByVal brush As Brush) As [Double]
            Dim color As New Color()
            Dim intensity As [Double] = 0
            If brush Is Nothing Then
                Return 1
            End If
            If brush.[GetType]().Name = "SolidColorBrush" Then
                color = TryCast(brush, SolidColorBrush).Color

                intensity = CType((color.R + color.G + color.B) / (3 * 255), Double)
            ElseIf brush.[GetType]().Name = "LinearGradientBrush" OrElse brush.[GetType]().Name = "RadialGradientBrush" Then

                For Each grad As GradientStop In TryCast(brush, GradientBrush).GradientStops
                    color = grad.Color
                    intensity += CType((color.R + color.G + color.B) / (3 * 255), Double)

                Next
                intensity /= TryCast(brush, GradientBrush).GradientStops.Count
            Else
                intensity = 0
            End If
            Return intensity
        End Function
#End Region

    End Class
End Namespace
